package client

import (
	"fmt"
	"time"

	"gitee.com/shiwjlinux/game-manager/common"
	"github.com/go-resty/resty/v2"
)

func NewClient(address string) *Client {
	c := resty.New().
		SetTimeout(10 * time.Second).
		SetRetryCount(3).
		SetRetryWaitTime(3 * time.Second)

	c.BaseURL = address
	c.Header.Add("Content-Type", "application/json")
	c.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
		if resp.StatusCode()/100 != 2 {
			// handle error
			// 	logrus.Infof("RunCommand Result: %+v", *ret)

			return fmt.Errorf(
				"response status code is not 2xx: %d ,body: %s",
				resp.StatusCode(),
				string(resp.Body()))
		}
		return nil
	})

	return &Client{
		C: c,
		// logger: conf.C().Log.Logger(),
	}
}

type Client struct {
	C *resty.Client
	// logger *zerolog.Logger
}

func (c *Client) Debug(v bool) {
	c.C.SetDebug(v)
}

func (c *Client) String() string {
	return common.ToJsonString(c)
}
